package com.ruoyi.project.infoprojectdepartment.controller;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.project.infoprojectdepartment.domain.InfoProjectDepartment;
import com.ruoyi.project.infoprojectdepartment.service.IInfoProjectDepartmentService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 协同科室项目关联Controller
 *
 * @author ruoyi
 * @date 2022-06-29
 */
@RestController
@RequestMapping("/project/department")
public class InfoProjectDepartmentController extends BaseController
{
    @Autowired
    private IInfoProjectDepartmentService infoProjectDepartmentService;

    /**
     * 查询协同科室项目关联列表
     */
    @PreAuthorize("@ss.hasPermi('system:department:list')")
    @GetMapping("/list")
    public TableDataInfo list(InfoProjectDepartment infoProjectDepartment)
    {
        startPage();
        List<InfoProjectDepartment> list = infoProjectDepartmentService.selectInfoProjectDepartmentList(infoProjectDepartment);
        return getDataTable(list);
    }

    /**
     * 导出协同科室项目关联列表
     */
    @PreAuthorize("@ss.hasPermi('system:department:export')")
    @Log(title = "协同科室项目关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InfoProjectDepartment infoProjectDepartment)
    {
        List<InfoProjectDepartment> list = infoProjectDepartmentService.selectInfoProjectDepartmentList(infoProjectDepartment);
        ExcelUtil<InfoProjectDepartment> util = new ExcelUtil<InfoProjectDepartment>(InfoProjectDepartment.class);
        util.exportExcel(response, list, "协同科室项目关联数据");
    }

    /**
     * 获取协同科室项目关联详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:department:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(infoProjectDepartmentService.selectInfoProjectDepartmentById(id));
    }

    /**
     * 新增协同科室项目关联
     */
    @PreAuthorize("@ss.hasPermi('system:department:add')")
    @Log(title = "协同科室项目关联", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InfoProjectDepartment infoProjectDepartment)
    {
        return toAjax(infoProjectDepartmentService.insertInfoProjectDepartment(infoProjectDepartment));
    }

    /**
     * 修改协同科室项目关联
     */
    @PreAuthorize("@ss.hasPermi('system:department:edit')")
    @Log(title = "协同科室项目关联", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InfoProjectDepartment infoProjectDepartment)
    {
        return toAjax(infoProjectDepartmentService.updateInfoProjectDepartment(infoProjectDepartment));
    }

    /**
     * 删除协同科室项目关联
     */
    @PreAuthorize("@ss.hasPermi('system:department:remove')")
    @Log(title = "协同科室项目关联", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(infoProjectDepartmentService.deleteInfoProjectDepartmentByIds(ids));
    }

    /**
     * 项目分配科室
     */
    @PostMapping("allocationDepartment")
    public AjaxResult allocationDepartment(@RequestBody JSONObject json)
    {
        Optional.ofNullable(json.get("departmentIds")).orElseThrow(() -> new RuntimeException("科室信息不能为空！"));
        Optional.ofNullable(json.get("projectIds")).orElseThrow(() -> new RuntimeException("项目id不能为空！"));
        return AjaxResult.success(infoProjectDepartmentService.saveProjectDepartment(json));
    }

    /**
     * 查询科室需协同的项目信息
     */
    @GetMapping("departmentProjectInfo")
    public TableDataInfo departmentProjectInfo(String projectName)
    {
      //  String projectName=json.getString("projectName");
        startPage();
       List<Map<String,Object>>  list=infoProjectDepartmentService.getDepartmentProjectList(projectName);
        return getDataTable(list);
    }
}
